package com.sg.business.commons.ui.work;

import java.util.HashMap;
import java.util.Iterator;

import org.eclipse.core.runtime.Assert;
import org.eclipse.rap.rwt.RWT;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

import com.mobnut.design.ICSSConstants;
import com.sg.widgets.Widgets;
import com.sg.widgets.part.IRefreshablePart;

public class AbstractDeliveryTabBlock extends Composite implements
		IRefreshablePart {
	public int TOPICSIZE = 30;
	private Composite topic;
	private Composite content;
	private HashMap<Integer, Control> pageMap;
	private HashMap<Integer, Control> buttonMap;
	private int currentId = -1;

	public AbstractDeliveryTabBlock(Composite parent) {
		super(parent, SWT.NONE);

		pageMap = new HashMap<Integer, Control>();
		buttonMap = new HashMap<Integer, Control>();
		setLayout(new FormLayout());
		topic = new Composite(this, SWT.NONE);
		topic.setBackground(Widgets.getColor(getDisplay(), 0xfc, 0xfc, 0xfc));
		topic.setData(RWT.CUSTOM_VARIANT, ICSSConstants.METRO_GRAY_30_ACTIVE);
		FormData fd = new FormData();
		topic.setLayoutData(fd);
		fd.top = new FormAttachment();
		fd.left = new FormAttachment();
		fd.right = new FormAttachment(100);
		fd.height = TOPICSIZE;
		RowLayout layout = new RowLayout();
		layout.marginBottom = 0;
		layout.marginTop = 0;
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		layout.marginLeft = 0;
		layout.marginRight = 0;
		layout.pack = true;
		layout.spacing = 0;
		layout.center = true;
		topic.setLayout(layout);

		Label sep = new Label(this, SWT.NONE);
		// sep.setBackground(Widgets.getColor(parent.getDisplay(), 0xed, 0xed,
		// 0xed));
		fd = new FormData();
		sep.setLayoutData(fd);
		fd.left = new FormAttachment();
		fd.top = new FormAttachment(topic);
		fd.right = new FormAttachment(100);
		fd.height = 1;

		content = new Composite(this, SWT.NONE);
		content.setBackground(parent.getDisplay().getSystemColor(
				SWT.COLOR_WHITE));
		fd = new FormData();
		content.setLayoutData(fd);
		fd.left = new FormAttachment();
		fd.top = new FormAttachment(sep);
		fd.right = new FormAttachment(100);
		fd.bottom = new FormAttachment(100);
		content.setLayout(new FormLayout());

	}

	public void addPage(final int id, String title, Image image) {
		Button go = new Button(topic, SWT.PUSH);
		go.setData(RWT.CUSTOM_VARIANT, ICSSConstants.METRO_GRAY_30_TAB);
		go.setText(title);
		if (image != null) {
			go.setImage(image);
		}
		RowData rd = new RowData();
		rd.height = TOPICSIZE;
		go.setLayoutData(rd);
		go.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				select(id);
			}
		});
		buttonMap.put(id, go);

		Label sep = new Label(topic, SWT.NONE);
		sep.setBackground(Widgets.getColor(topic.getDisplay(), 0xed, 0xed, 0xed));
		rd = new RowData();
		rd.height = 24;
		rd.width = 1;
		sep.setLayoutData(rd);
	}

	public void select(int id) {
		if (id == currentId) {
			return;
		}
		currentId = id;

		Control control = pageMap.get(new Integer(id));
		if (control == null || control.isDisposed()) {
			control = createPage(id, content);
			Assert.isNotNull(control);
			FormData fd = new FormData();
			control.setLayoutData(fd);
			fd.top = new FormAttachment();
			fd.left = new FormAttachment();
			fd.right = new FormAttachment(100);
			fd.bottom = new FormAttachment(100);
			pageMap.put(new Integer(id), control);
		}

		Iterator<Control> iter = pageMap.values().iterator();
		while (iter.hasNext()) {
			Control page = iter.next();
			if (page == control) {
				page.setVisible(true);
				page.moveAbove(null);
			} else {
				page.setVisible(false);
				page.moveBelow(null);
			}
		}

		Iterator<Integer> iter2 = buttonMap.keySet().iterator();
		while (iter2.hasNext()) {
			Integer _id = iter2.next();
			control = buttonMap.get(_id);
			control.setData(
					RWT.CUSTOM_VARIANT,
					_id.intValue() == id ? ICSSConstants.METRO_GRAY_30_TAB_ACTIVE
							: ICSSConstants.METRO_GRAY_30_TAB);
		}
		// topic.layout();
		content.layout(false, false);

	}

	public Control createPage(int pageId) {
		return createPage(pageId, content);
	}

	public Control createPage(int pageId, Composite parent) {
		return null;
	}

	@Override
	public void doRefresh() {
		Iterator<Integer> iter = pageMap.keySet().iterator();
		while (iter.hasNext()) {
			Integer _id = iter.next();
			Control page = pageMap.get(_id);
			pageRefresh(_id.intValue(), page);
		}
	}

	public void pageRefresh(int pageId, Control page) {

	}

	@Override
	public boolean canRefresh() {
		return true;
	}
}
